import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/login.vue'
import Home from '@/components/Home.vue'
import User from '@/components/User.vue'
import Roles from '@/components/Roles.vue'
import Rights from '@/components/Rights.vue'
import Goods from '@/components/Goods.vue'
import Params from '@/components/Params.vue'
import Categories from '@/components/Categories'
import Orders from '@/components/Orders.vue'
import AddGoods from '@/components/AddGoods.vue'
import Report from '@/components/Reports.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/', redirect: '/login'
  },
  {
    path: '/login', component: Login
  },

  {
    path: '/home',
    component: Home,
    children: [{
      path: '/users',
      component: User
    }, {
      path: '/roles',
      component: Roles
    }, {
      path: '/rights',
      component: Rights
    }, {
      path: '/goods',
      component: Goods
    }, {
      path: '/params',
      component: Params
    }, {
      path: '/categories',
      component: Categories
    }, {
      path: '/orders',
      component: Orders
    },
    {
      path: '/addGoods', component: AddGoods
    },
    {
      path: '/reports', component: Report
    }],
    redirect: '/users'
  }

]

const router = new VueRouter({
  routes
})
// 为路由对象添加beforeEach导航守卫
router.beforeEach(function (to, from, next) {
  var path = to.path
  if (path === '/home') {
    // 从sessionStorage中获取到token值
    const token = window.sessionStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
